package com.dqyt.app.mongo;

import com.dqyt.app.bean.LocaGzlqrd;
import com.dqyt.app.bean.Maintenance;
import com.dqyt.app.bean.Report;
import com.dqyt.app.bean.Sample;
import com.mongodb.client.result.DeleteResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class LocaGzlqrdDao {


    /**
     * 注入MongoTemplate
     */
    @Autowired
    protected MongoTemplate mongoTemplate;



    /**
     * 新建数据到数据库
     *
     * @return
     */
    public void save(LocaGzlqrd locaGzlqrd) {
        mongoTemplate.save(locaGzlqrd); // 保存数据到数据库
    }



    public LocaGzlqrd getLocaGzlqrdById(String rwdbh){
        // 构建查询条件
        Query query = new Query();
        query.addCriteria(new Criteria("rwdbh").is(rwdbh));
        List<LocaGzlqrd> list = mongoTemplate.find(query, LocaGzlqrd.class, LocaGzlqrd.TableName); // 取得满足条件的数据
        if(list.size()>0){
            return list.get(0);
        }else{
            return null;
        }

    }


    /**
     * 分页查询本地 工作量确认单
     * @param pageNum
     * @param pageSize
     * @param rwdbh
     * @param wtfId
     * @param wtfchId
     * @return
     */
    public List<LocaGzlqrd>  getLocaGzlqrdPage(int pageNum, int pageSize, String rwdbh, int wtfId,int wtfchId,int start){
        // 构建查询条件
        Query query = new Query();
        if (null != rwdbh && !rwdbh.equals("")) {
            query.addCriteria(new Criteria("rwdbh").is(rwdbh));
        }
        if(wtfId>0){
            query.addCriteria(new Criteria("wtfId").is(wtfId));
        }
        if(wtfchId !=0 ){
            query.addCriteria(new Criteria("wtfchId").is(wtfchId));
        }
        if(start !=0){
            query.addCriteria(new Criteria("start").is(start));
        }

        query.with(Sort.by(Sort.Order.desc("rwdbh")));
        query.limit(pageSize).skip(pageNum);
        List<LocaGzlqrd> list = mongoTemplate.find(query, LocaGzlqrd.class, LocaGzlqrd.TableName); // 取得满足条件的数据
        return list;
    }


    /**
     * 查询总条数
     * @param rwdbh
     * @param wtfId
     * @param wtfchId
     * @return
     */
    public long  getLocaGzlqrdTotalNumber(String rwdbh, int wtfId,int wtfchId,int start){
        // 构建查询条件
        Query query = new Query();
        if (null != rwdbh && !rwdbh.equals("")) {
            query.addCriteria(new Criteria("rwdbh").is(rwdbh));
        }
        if(wtfId>0){
            query.addCriteria(new Criteria("wtfId").is(wtfId));
        }
        if(wtfchId !=0 ){
            query.addCriteria(new Criteria("wtfchId").is(wtfchId));
        }
        if(start !=0){
            query.addCriteria(new Criteria("start").is(start));
        }

        query.with(Sort.by(Sort.Order.desc("jsrq")));
        List<LocaGzlqrd> list = mongoTemplate.find(query, LocaGzlqrd.class, LocaGzlqrd.TableName); // 取得满足条件的数据
        return list.size();
    }





    /**
     * 分页查询本地 工作量确认单  待批准的
     * @param pageNum
     * @param pageSize
     * @param rwdbh
     * @param wtfId
     * @param wtfchId
     * @return
     */
    public List<LocaGzlqrd>  getLocaGzlqrdDpzPage(int pageNum, int pageSize, String rwdbh, int wtfId,int wtfchId){
        // 构建查询条件
        Query query = new Query();
        if (null != rwdbh && !rwdbh.equals("")) {
            query.addCriteria(new Criteria("rwdbh").is(rwdbh));
        }
        if(wtfId>0){
            query.addCriteria(new Criteria("wtfId").is(wtfId));
        }
        if(wtfchId !=0 ){
            query.addCriteria(new Criteria("wtfchId").is(wtfchId));
        }
        query.addCriteria(new Criteria("start").is(1));

        query.with(Sort.by(Sort.Order.desc("jsrq")));
        query.limit(pageSize).skip(pageNum);
        List<LocaGzlqrd> list = mongoTemplate.find(query, LocaGzlqrd.class, LocaGzlqrd.TableName); // 取得满足条件的数据
        return list;
    }


    /**
     * 查询总条数   待批准的
     * @param rwdbh
     * @param wtfId
     * @param wtfchId
     * @return
     */
    public long  getLocaGzlqrdDpzTotalNumber(String rwdbh, int wtfId,int wtfchId){
        // 构建查询条件
        Query query = new Query();
        if (null != rwdbh && !rwdbh.equals("")) {
            query.addCriteria(new Criteria("rwdbh").is(rwdbh));
        }
        if(wtfId>0){
            query.addCriteria(new Criteria("wtfId").is(wtfId));
        }
        if(wtfchId !=0 ){
            query.addCriteria(new Criteria("wtfchId").is(wtfchId));
        }
        query.with(Sort.by(Sort.Order.desc("jsrq")));
        List<LocaGzlqrd> list = mongoTemplate.find(query, LocaGzlqrd.class, LocaGzlqrd.TableName); // 取得满足条件的数据
        return list.size();
    }


    /**
     * 删除本地工作量确认单
     *
     * @return
     */
    public long deleteById(String rwdbh) {
        Query query = Query.query(Criteria.where("rwdbh").is(rwdbh));
        DeleteResult deleteResult = mongoTemplate.remove(query, LocaGzlqrd.class);
        long res = deleteResult.getDeletedCount();
        return res;
    }



    /**
     * 分页查询本地 工作量确认单
     * @param rwdbh
     * @param wtfId
     * @param wtfchId
     * @return
     */
    public List<LocaGzlqrd>  getLocaGzlqrdTj(String rwdbh, int wtfId,int wtfchId,int start){
        // 构建查询条件
        Query query = new Query();
        if (null != rwdbh && !rwdbh.equals("")) {
            query.addCriteria(new Criteria("rwdbh").is(rwdbh));
        }
        if(wtfId>0){
            query.addCriteria(new Criteria("wtfId").is(wtfId));
        }
        if(wtfchId !=0 ){
            query.addCriteria(new Criteria("wtfchId").is(wtfchId));
        }
        if(start !=0){
            query.addCriteria(new Criteria("start").is(start));
        }
        query.with(Sort.by(Sort.Order.desc("rwdbh")));
        List<LocaGzlqrd> list = mongoTemplate.find(query, LocaGzlqrd.class, LocaGzlqrd.TableName); // 取得满足条件的数据
        return list;
    }

}
